<?php
/**
 *
 * 版权所有：忠赢网站管理系统<www.91zoy.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class CaseController extends ComController
{

    public function add()
    {
        $category = M('acategory')->field('id,name')->order('o asc')->select();
       // var_dump($category);exit;
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, 0);
        $this->assign('category', $category);//导航

        // 查出所有案例
        $product = M('case')->field("id,title")->select();
        // 顾问下所有产品
        // dump($product);
        $this->assign('product',$product);
        $this->display('form');
    }

    public function index($id = 0, $p = 1)
    {
        $p = intval($p) > 0 ? $p : 1;
        $case = M('case');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $sid = isset($_GET['id']) ? $_GET['id'] : '';
        $keyword = isset($_GET['keyword']) ? htmlentities($_GET['keyword']) : '';
        $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
        $where = '1 = 1 ';
        if ($sid) {
            $sids_array = category_get_sons($sid);
            $sids = implode(',',$sids_array);
            $where .= "and {$prefix}case.id in ($ids) ";
        }
        if ($keyword) {
            $where .= "and {$prefix}case.title like '%{$keyword}%' ";
        }
        //默认按照时间降序
        $orderby = "time desc";
        if ($order == "asc") {
            $orderby = "time asc";
        }
        //获取栏目分类
        $category = M('acategory')->field('id,name')->order('o asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $sid);
        $this->assign('category', $category);//导航


        $count = $case->where($where)->count();
        $list = $case->field("{$prefix}case.*,{$prefix}acategory.name")->where($where)->order($orderby)->join("{$prefix}acategory ON {$prefix}acategory.id = {$prefix}case.aid")->limit($offset . ',' . $pagesize)->select();
        //var_dump($list);exit;
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del()
    {

        $aids = isset($_REQUEST['aids']) ? $_REQUEST['aids'] : false;
       // var_dump($aids);exit;
        if ($aids) {
            if (is_array($aids)) {
                $aids = implode(',', $aids);
                $map['id'] = array('in', $aids);
            } else {
                $map = 'id=' . $aids;
            }
            if (M('case')->where($map)->delete()) {
                addlog('删除案例，AID：' . $aids);
                $this->success('恭喜，案例删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function edit($id)
    {

        $aid = intval($id);
        //var_dump($aid);exit;
        $case = M('case')->where('id=' . $id)->find();
        if ($case) {

            $category = M('acategory')->field('id,name')->order('o asc')->select();
            $tree = new Tree($category);
            $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
            $category = $tree->get_tree(0, $str);
            $this->assign('category', $category);//导航
            $this->assign('case', $case);
        } else {
            $this->error('参数错误！');
        }
        $this->display('form');
    }

    public function update($id = 0)
    {
        $id = $_POST['id'];
       // var_dump($id);exit;
        $aid = I('post.aid', '', 'strip_tags');
        $acategory = M("acategory")->where(array('id'=>$aid))->find();
        //var_dump($acategory);exit;
        $data['aid'] = isset($_POST['aid']) ? intval($_POST['aid']) : 0;
        $data['title'] = isset($_POST['title']) ? $_POST['title'] : false;
        $data['seotitle'] = isset($_POST['seotitle']) ? $_POST['seotitle'] : '';
        $data['keywords'] = I('post.keywords', '', 'strip_tags');
        $data['project'] = I('post.project', '', 'strip_tags');
        $data['mod'] = I('post.mod', '', 'strip_tags');
        $data['dir'] = $acategory['dir'];
        $data['description'] = I('post.description', '', 'strip_tags');
        $data['content'] = html_entity_decode(isset($_POST['content']) ? $_POST['content'] : false);
        $data['thumbnail'] = I('post.thumbnail', '', 'strip_tags');
        $data['time'] = time();
        // var_dump($data);exit;
        if (!$data['aid'] or !$data['title'] or !$data['content']) {
            $this->error('警告！案例分类、案例标题及案例内容为必填项目。');
        }
        if ($id) {
            M('case')->data($data)->where('id=' . $id)->save();
            
            addlog('编辑文章，ID：' . $id);
            $this->success('恭喜！案例编辑成功！');
        } else {
            $id = M('case')->data($data)->add();
            if ($id) {
                addlog('新增文章，ID：' . $id);
                $this->success('恭喜！案例新增成功！');
            } else {
                $this->error('抱歉，未知错误！');
            }

        }
    }
    //新增案例分类管理
    public function category(){
        $category = M('acategory')->field('id,name')->order('o asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str);

        $this->assign('category', $category);
        $this->display('category');
    }
    //新增分类管理列表
    public function category_index(){
        $category = M('acategory')->field('id,name,o')->order('o asc')->select();
        $category = $this->getMenu($category);
        $this->assign('category', $category);
        $this->display('category_index');
    }
    //删除分类
     public function category_del()
    {

        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $data['id'] = $id;
            $category = M('acategory');
                $category->where('id=' . $id)->delete();
                addlog('删除分类，ID：' . $id);
                $this->success('恭喜，分类删除成功！',U('/Qwadmin/Case/category_index'),3);
            die('1');
        } else {
            die('0');
        }

    }
    public function category_edit()
    {

        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        $currentcategory = M('acategory')->where('id=' . $id)->find();
        $this->assign('currentcategory', $currentcategory);

        $category = M('acategory')->field('id,name')->where("id <> {$id}")->order('o asc')->select();
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str);

        $this->assign('category', $category);
        $this->display('category');
    }
    public function category_update($act = null)
    {
        if ($act == 'order') {
            $id = I('post.id', 0, 'intval');
            if (!$id) {
                die('0');
            }
            $o = I('post.o', 0, 'intval');
            M('acategory')->data(array('o' => $o))->where("id='{$id}'")->save();
            addlog('分类修改排序，ID：' . $id);
            die('1');
        }

        $id = I('post.id', false, 'intval'); 
        $data['name'] = I('post.name');
        $data['dir'] = I('post.dir','',array('strip_tags','trim'));
        // $data['seotitle'] = I('post.seotitle', '', 'htmlspecialchars');
        // $data['keywords'] = I('post.keywords', '', 'htmlspecialchars');
        // $data['description'] = I('post.description', '', 'htmlspecialchars');
        // $data['cattemplate'] = I('post.cattemplate');
        // $data['contemplate'] = I('post.contemplate');
        $data['o'] = I('post.o', 0, 'intval');
        if ($data['name'] == '') {
            $this->error('分类名称不能为空！');
        }
        if ($id) {
            if (M('acategory')->data($data)->where('id=' . $id)->save()) {
                addlog('文章分类修改，ID：' . $id . '，名称：' . $data['name']);
                $this->success('恭喜，分类修改成功！',U('/Qwadmin/Case/category_index'),3);
                die(0);
            }
        } else {
            $id = M('acategory')->data($data)->add();
            if ($id) {
                addlog('新增分类，ID：' . $id . '，名称：' . $data['name']);
                $this->success('恭喜，新增分类成功！',U('/Qwadmin/Case/category_index'),3);
                die(0);
            }
        }
        $this->success('恭喜，操作成功！');
    }
}